package magicloan.auctionpocket.ui.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.json.JSONException;
import org.json.JSONObject;

import magicloan.auctionpocket.bean.UserData;
import magicloan.auctionpocket.net.NetService;
import magicloan.auctionpocket.net.OnNetCallBack;

/**
 * Created by hechao on 2018/1/25.
 */

public abstract class BaseFragment extends Fragment implements View.OnClickListener , OnNetCallBack {

    public Context mContext;
    public Activity mActivity;
    public UserData instance ;
    protected NetService mService ;
    protected ViewGroup mContentView;//缓存Fragment view
    protected View mViewEmpty ;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = mActivity = this.getActivity();
        mService = new NetService(this,mActivity);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mContentView == null) {
            beforeInitView();
            mContentView = (ViewGroup) inflater.inflate(setFgContentView(), null);
            initView();
            afterInitView();
            initData();
        }
        //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) mContentView.getParent();
        if (parent != null) {
            parent.removeView(mContentView);
        }
        instance = UserData.getInstance() ;
        return mContentView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    protected abstract int setFgContentView();

    protected abstract void initView();

    protected abstract void beforeInitView();

    protected abstract void afterInitView();

    protected abstract void initData();

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onStop() {
        super.onStop();
    }



    public void afterConnectSuccess(String flag, JSONObject request) throws JSONException {

    }

    public void afterConnectFail(String flag, JSONObject result,String cache) throws JSONException {

    }

    public void bindData2View(){

    }


}
